package util;

import beans.ChessPiece;
import beans.Color;
import beans.Piece;
import beans.Position;

/**
 * Generic class with useful static methods
 * 
 * @author Josep M Sobrepere
 */
public class Util {	
	/**
	 * @param pieceInitial the initial of the piece
	 * @return the equivalent piece
	 */
	public static ChessPiece getChessPieceFromInitial(char pieceInitial){
		switch (pieceInitial){
		case 'R' : 
			return ChessPiece.ROOK;			
		case 'N' :
			return ChessPiece.KNIGHT;			
		case 'B' :
			return ChessPiece.BISHOP;			
		case 'Q' :
			return ChessPiece.QUEEN;			
		case 'K' :
			return ChessPiece.KING;
		}
		return null;
	}
	
	public static char getFENInitial(Piece piece){		
		switch (piece.getColor()){
		case WHITE :
			switch (piece.getKind()){
			case ROOK :
				return 'R';							
			case KNIGHT:
				return 'N';
			case BISHOP:
				return 'B';
			case QUEEN:
				return 'Q';							
			case KING:
				return 'K';
			case PAWN:
				return 'P';
			}
		case BLACK :
			switch (piece.getKind()){
			case ROOK :
				return 'r';
			case KNIGHT:
				return 'n';
			case BISHOP:
				return 'b';
			case QUEEN:
				return 'q';
			case KING:
				return 'k';
			case PAWN:
				return 'p';
			}
		}
		return 0;
	}
	
	/**
	 * Retrieves a Position from a string representation in algebraic notation, for example
	 * "a1" would be the position 0,0. 
	 * 
	 * @param str the algebraic representation of the position
	 * @return the equivalent position
	 */
	public static Position getPositionFromStr(String str){
		int x = str.charAt(0)-97;
		int y = Integer.parseInt(str.substring(1));
		y--;
		return new Position(x, y);
	}
	
	/**
	 * Retrieves a string representation in algebraic notation from a Position bean, for example
	 *  the position 0,0 woulg be "a1" in algebraic notation.
	 * 
	 * @param pos the input position
	 * @return the algebraic representation
	 */
	public static String getStrFromPosition(Position pos){		
		return "" + ((char) (pos.getX()+97)) + ((char) (pos.getY()+49)) ;
	}
}
